const Router=require('koa-router');
const poem=new Router(); 
const sqldata = require('../sqldata');
//获得全部古诗
poem.get('/',async(ctx)=>{
    let sql=`select * from works`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=JSON.parse(JSON.stringify(mydata))
})
//增加古诗
poem.post('/',async(ctx)=>{
    let val=ctx.request.body
    let sql;
    let mydata6=await new Promise((resolve, reject)=>{
        sql=`select * from works where wid=${val.wid}`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(mydata6){
        let mydata7 = await new Promise((resolve, reject)=>{
            sql=`update works set wtitle='${val.wtitle}',wdynasty='${val.wdynasty}',aid=${mydata1.aid},wcontent='${val.wcontent}',wappreciation='${val.wappreciation}',wtranslate='${val.wtranslate}',wtag='${val.wtag}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        let mydata1 = await new Promise((resolve, reject)=>{
            sql=`select * from authorfoot where aname='${val.aname}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        if(mydata1){   
            let mydata2 = await new Promise((resolve, reject)=>{
                sql=`insert into works (wtitle,wdynasty,aid,wcontent,wappreciation,wtranslate,wtag) values ('${val.wtitle}','${val.wdynasty}',${mydata1.aid},'${val.wcontent}','${val.wappreciation}','${val.wtranslate}','${val.wtag}')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })   
        }else{
            let mydata3 = await new Promise((resolve, reject)=>{
                sql=`insert into authorfoot (aname,adynasty) values ('${val.aname}','${val.wdynasty}')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            let mydata4=await new Promise((resolve, reject)=>{
                sql=`select * from authorfoot where aname='${val.aname}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            let mydata5 = await new Promise((resolve, reject)=>{
                sql=`insert into works (wtitle,wdynasty,aid,wcontent,wappreciation,wtranslate,wtag) values ('${val.wtitle}','${val.wdynasty}','${mydata4.aid}','${val.wcontent}','${val.wappreciation}','${val.wtranslate}','${val.wtag}')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
    }
})
//删除古诗
poem.delete('/',async(ctx)=>{
    let val=ctx.request.body
    let sql=`delete from works where wid=${val.wid}`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
})
module.exports=poem;